// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import directValidate from './utils/directValidate'
Vue.config.productionTip = false

/* eslint-disable no-new */


Vue.directive('format',{
  twoWay: true,
    // 指令的定义
    inserted: function (el) {
      console.log("========inserted========")
      //el.focus()
    },
    bind:function(el,binding){
      console.log(el)
      console.log("========bind========"+binding.value)
      console.log("arg===>"+binding.arg);
      el.value = "bind==>"+(binding.value == 0?'女':'男');
    },
    update:function(el, binding){  //更新时
      console.log("=======update========")
      el.value = "update==>"+binding.value;
      el.value = "bind==>"+(binding.value == 0?'女':'男');
    },
      // 所在组件的 VNode 及其孩子的 VNode 全部更新时调用。
    componentUpdated(){
      console.log( 'componentUpdated' );
    },
    // 只调用一次，指令与元素解绑时调用。
    unbind(){
      console.log( 'unbind' );
     // this.el.removeEventListener('input', this.handler)
    }
})
// const PHONE_REGEX  =  /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/; //手机号 direct 
// const IDCard_REGEX =  /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;  //身份证
// const EMAIL_REGEX = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; // email
/*const REGEX = [
  {name:'phone',regx:/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/}, //手机号
  {name:'idcard',regx:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/},           //身份证
  {name:'email',regx:/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/}, //email
]
Vue.directive('checkParam', {
  inserted: function (el, binding, vNode) {
    el.addEventListener('keyup', function (event) {
        let requiredMsg = binding.value.requiredMsg; //非空判断
        let dataType = binding.value.dataType //数据类型 姓名，身份证，手机号码,邮箱，
        let regexMsg = binding.value.regexMsg //数据类型 提示正则
        let regex = binding.value.regex;  //自定义正则
         el.setAttribute('error-msg','')
        if(requiredMsg){
          if (!el.value || el.value === '') {
            el.setAttribute('error-msg',requiredMsg)
            return;
          }
        }
       /* if(dataType){  //自己定义数据类型
            for(let m in REGEX){
              if(dataType == REGEX[m].name){
                    if (!el.value.match(REGEX[m].regx)) {
                         el.setAttribute('error-msg',regexMsg);
                        return;
                    }
              }
            }
        }
        if(regex){  //自定义正则校验
          let mREGEX = REGEX.filter(u=>u.name == regex) //判断是否 是自定义数据类型
          if(mREGEX[0]){
              regex = mREGEX[0].regx
          }
          if (!el.value.match(regex)) {
              el.setAttribute('error-msg',regexMsg);
              return;
          }
        }
    })
  }
});
Vue.directive('checkSubmit', {
  inserted: function (el, binding, vNode) {
    el.addEventListener('click', function (event) {
        let eles = document.getElementsByTagName('input');
        let isVaile = true;
        var evObj = document.createEvent('Event')
        evObj.initEvent('keyup', true, true)
        for(let key in eles){
           if(key>=0){
              eles[key].dispatchEvent(evObj)
           }
          }
        for(let key in eles){
          if(key>=0){
            let errorMsg = eles[key].getAttribute('error-msg');
            if(errorMsg){
              isVaile = false;
              alert(errorMsg)
              return;
              console.log(key+"errorMsg==>"+errorMsg)
            }
          }
        }
        // if(!isVaile){
        //     return;
        // }
      
        vNode.context.submit();
    })
  }
});*/

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})